package com.goodfox.web.generic.account.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.generic.account.entity.Account;
import com.goodfox.web.generic.account.mapper.AccountMapper;
import com.goodfox.web.generic.account.service.AccountService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;
import com.goodfox.web.generic.provide.entity.Pager;
import com.goodfox.web.generic.provide.util.DateUtil;
import com.goodfox.web.generic.provide.util.ParameterUtil;
import com.goodfox.web.generic.role.entity.Role;

@Transactional
@Service(value="accountService")
@NoteExplain(comment="人员账户管理")
@RemotingDestination(channels={"my-amf","my-secure-amf"})
public class AccountServiceImpl implements AccountService {

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(AccountServiceImpl.class);

	/** 账户Mapper */
	@Autowired
	private AccountMapper accountMapper;
	
	@NoteExplain(comment="查询人员账号分页列表")
	public Pager<Account> queryAccountPager(int currentPage,int numPerPage, Map map) {
		int start=(currentPage-1)*numPerPage;
		Long allCount=accountMapper.selectAccountListCount(map);
		List<Account> list=accountMapper.selectAccountList(map,new RowBounds(start, numPerPage));
		return new Pager<Account>(allCount,list);
	}

	@NoteExplain(comment="新增人员账号",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addAccount(Account entity,String loginLogId){
		if(entity.getMemIdCard()!=null && !entity.getMemIdCard().equals("")){
			String memBirthday="";
			if(entity.getMemIdCard().length()==18){
				memBirthday=entity.getMemIdCard().substring(6,10)+"-"+entity.getMemIdCard().substring(10,12)+"-"+entity.getMemIdCard().substring(12,14);
			}else if(entity.getMemIdCard().length()==15){
				memBirthday="19"+entity.getMemIdCard().substring(6,8)+"-"+entity.getMemIdCard().substring(8,10)+"-"+entity.getMemIdCard().substring(10,12);
			}
			if(!memBirthday.equals("")){
				entity.setMemBirthday(DateUtil.dateFormat(memBirthday, "yyyy-MM-dd"));
			}
		}
		Md5PasswordEncoder md5=new Md5PasswordEncoder();
		entity.setActPassword(md5.encodePassword(entity.getActPassword(), null));
		entity.setActState("1");
		accountMapper.insertAccount(entity);
	}
	
	@NoteExplain(comment="更新人员账号",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyAccount(Account oldEny,Account newEny,String loginLogId){
		if(newEny.getMemIdCard()!=null && !newEny.getMemIdCard().equals("")){
			String memBirthday="";
			if(newEny.getMemIdCard().length()==18){
				memBirthday=newEny.getMemIdCard().substring(6,10)+"-"+newEny.getMemIdCard().substring(10,12)+"-"+newEny.getMemIdCard().substring(12,14);
			}else if(newEny.getMemIdCard().length()==15){
				memBirthday="19"+newEny.getMemIdCard().substring(6,8)+"-"+newEny.getMemIdCard().substring(8,10)+"-"+newEny.getMemIdCard().substring(10,12);
			}
			if(!memBirthday.equals("")){
				newEny.setMemBirthday(DateUtil.dateFormat(memBirthday, "yyyy-MM-dd"));
			}
		}
		newEny.setActState("1");
		accountMapper.updateAccount(newEny);
	}
	
	@NoteExplain(comment="移除人员账号",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeAccount(Account entity,String loginLogId){
		accountMapper.updateDeleteAccountById(entity.getActId());
	}

	@NoteExplain(comment="获取人员账号")
	public Account getAccount(String actId) {
		return accountMapper.selectAccountById(actId);
	}

	@NoteExplain(comment="系统内是否存在该账号")
	public boolean isHave(String actName) {
		boolean b=false;
		try{
			String actId=accountMapper.selectActIdByName(actName);
			if(null!=actId){
				b=true;
			}
		}catch(Exception ex){
			logger.error("根据人员名称获取人员异常：",ex);
		}
		return b;
	}
	
	@NoteExplain(comment="获取人员账号",paraDesc="actName 用户名;actPassword 密码")
	public Account getAccountByNamePwd(String actName, String actPassword){
		Map map = new HashMap();
		map.put("actName", actName);
		Md5PasswordEncoder md5=new Md5PasswordEncoder();
		map.put("actPassword", md5.encodePassword(actPassword, null));
		Account account=accountMapper.selectAccountByNamePwd(map);
		return account;
	}
	
	@NoteExplain(comment="修改登录密码",secret=true,paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyPassword(Account oldEny,Account newEny,String loginLogId){
		Md5PasswordEncoder md5=new Md5PasswordEncoder();
		newEny.setActPassword(md5.encodePassword(newEny.getActPassword(), null));
		accountMapper.updatePassword(newEny);
	}

	
	@NoteExplain(comment="查询角色列表")
	public List<Role> queryRoleListByScope(String roleScope){
		Map map=new HashMap();
		map.put("roleScope", roleScope);
		return accountMapper.queryRoleListByScope(map);
	}
	
	@NoteExplain(comment="导出人员账号列表")
	public List<Account> exportAccountList(Map map){
		List<String> actIdList=ParameterUtil.getListForString(map.get("actIds").toString(), ";");
		if(actIdList.size()>0){
			map.put("actIdList", actIdList);
		}
		return accountMapper.selectAccountList(map);
	}
}
